.POSIX:


#
# Special targets
#

all:

.PHONY: analysis clean dist distclean install install-comp install-man \
	install-strip install-module installdirs maintainer-clean \
	mostlyclean sigdist venvinstall pre-install post-uninstall \
	uninstall uninstall-module uninstallfiles userinstall

.SUFFIXES:


#
# Macros
#

SHELL = /bin/sh

VPATH = .

srcdir = .


#
# Installation
#

INSTALL = install

PYTHON = python3

PIP = $(PYTHON) -mpip

VENV = $(PYTHON) -mvenv

PIPARGS = --root=$(DESTDIR) --prefix=$(prefix)

DESTDIR =

PREFIX = /usr/local

module = sievemgr

prefix = $(PREFIX)

exec_prefix = $(prefix)

datarootdir = $(prefix)/share

bindir = $(exec_prefix)/bin

mandir = $(datarootdir)/man

man1dir = $(mandir)/man1

man5dir = $(mandir)/man5

sysconfdir = /etc

man1ext = .1

man5ext = .5

man1-sievemgr = $(man1dir)/sievemgr$(man1ext)

man5-sieverc = $(man5dir)/sieverc$(man5ext)

bashcompdir = $(datarootdir)/bash-completion/completions

venvdir = /opt/odkr/sievemgr

dirs = '$(DESTDIR)$(man1dir)' '$(DESTDIR)$(man5dir)' '$(DESTDIR)$(bashcompdir)'

install: pre-install install-module install-man install-comp

install-man: installdirs $(DESTDIR)$(man1-sievemgr) $(DESTDIR)$(man5-sieverc)

uninstall: uninstall-module uninstallfiles uninstalldirs post-uninstall

install-comp: $(DESTDIR)$(bashcompdir)/sievemgr.bash

install-strip: install

install-module:
	$(NORMAL_INSTALL)
	$(PIP) install $(PIPARGS) .
	printf 'pip: %s\n' '$(PIP)' >>install.log

pre-install:
	$(PRE_INSTALL)
	set -C && : >install.log

installdirs:
	$(PRE_INSTALL)
	mkdir -pm0755 $(dirs)
	printf 'dir: %s\n' >>install.log $(dirs)

userinstall:
	$(MAKE) -e PIPARGS=--user prefix="$$HOME/.local" install

venvinstall:
	$(MAKE) -e PIP=$(venvdir)/bin/pip PIPARGS= PREFIX='$(venvdir)' install

uninstall-module:
	$(NORMAL_UNINSTALL)
	$$(sed -n 's/pip: //p' install.log) uninstall -y $(module)

uninstalldirs:
	$(POST_UNINSTALL)
	- sed -n 's/^dir: //p' install.log | xargs -E '' rmdir -pv

uninstallfiles:
	$(POST_UNINSTALL)
	sed -n 's/^file: //p' install.log | xargs -E '' rm -f

post-uninstall:
	$(POST_UNINSTALL)
	rm install.log

$(DESTDIR)$(bashcompdir)/sievemgr.bash: comp/sievemgr.bash
	$(POST_INSTALL)
	$(INSTALL) -m u=rw,go=r comp/sievemgr.bash '$@'
	printf 'file: %s\n' '$@' >>install.log

$(DESTDIR)$(man1-sievemgr): man/sievemgr.1
	$(POST_INSTALL)
	$(INSTALL) -m u=rw,go=r man/sievemgr.1 '$@'
	printf 'file: %s\n' '$@' >>install.log

$(DESTDIR)$(man5-sieverc): man/sieverc.5
	$(POST_INSTALL)
	$(INSTALL) -m u=rw,go=r man/sieverc.5 '$@'
	printf 'file: %s\n' '$@' >>install.log


#
# Documentation
#

man/sievemgr.1: docs/sievemgr.rst

man/sieverc.5: docs/sieverc.rst

man/sievemgr.1.gz: man/sievemgr.1

man/sieverc.5.gz: man/sieverc.5

man/sievemgr.1 man/sieverc.5:
	sphinx-build -Qbman docs man $@


#
# Distribution
#

GPG = gpg

TAR = tar -c

UNTAR = tar -x

ZIP = gzip

UNZIP = gunzip

version = 0.6.1

distname = $(module)-$(version)

distar = $(distname).tgz

distfiles = .flake8 .mypy.ini .pylintrc \
	comp/sievemgr.bash \
	docs/Makefile docs/conf.py \
	docs/changelog.rst docs/feedback.rst docs/index.rst docs/install.rst \
	docs/licence.rst docs/module.rst docs/packaging.rst docs/security.rst \
	docs/sievemgr.rst docs/sieverc.rst \
	man/sievemgr.1 man/sieverc.5 \
	INSTALL.rst LICENCE.rst NEWS.rst README.rst \
	makefile pyproject.toml \
	sievemgr.py

dist: $(distar)

sigdist: dist $(distar).asc

$(distar): $(distname)
	$(TAR) $(distname) | $(ZIP) >$(distar)

$(distar).asc: $(distar)
	$(GPG) -qab --batch --yes $(distar)

$(distname): $(distfiles)
	find $(distfiles) -exec pathchk -Pp "{}" +
	find $(distfiles) -exec dirname '{}' ';' | sort -u | \
		sed 's/^/$(distname)\//' | xargs -E '' mkdir -p
	find $(distfiles) -exec cp '{}' $(distname)/'{}' ';'
	[ "$$(uname)" = Darwin ] && xattr -cr $(distname)


#
# Cleanup
#

clean: mostlyclean

distclean: clean

maintainer-clean: distclean

clean:
	rm -rf build $(module).egg-info

distclean:
	rm install.log

maintainer-clean:
	rm -f makefile man/*

mostlyclean:
	rm -rf $(distname)
	rm -f $(distar) $(distar).asc


#
# Code analysis
#

analysis:
	mypy sievemgr.py      || [ $$? -eq 127 ]
	ruff sievemgr.py      || [ $$? -eq 127 ]
	flake8 sievemgr.py    || [ $$? -eq 127 ]
	pylint sievemgr.py    || [ $$? -eq 127 ]
	bandit -q sievemgr.py || [ $$? -eq 127 ]

